//引入集合模型
const { questionsModel } = require("../models/questionsModel");
const { knowledgePointsModel } = require("../models/knowledgePointsModel");
const { examTypeModel } = require("../models/examTypeModel");
const { testsModel } = require("../models/testsModel");
const { usersModel } = require("../models/usersModel");
const { completedTestModel } = require("../models/completedTestModel");




//该类型下已完成和未完成

async function isOkList(req, res, next) {
    // 该类型和用户id和下拉框选择（是否完成）

    const { TypeId, user_id, checkBox } = req.body;
    // 该类型下的试卷
    let allTestsArr = await testsModel.find({ TypeId }).populate('KnowledgePointId').populate('TypeId');

    //该用户完成的所有试卷
    let allCompletedTestsArr = await completedTestModel.find({ user_id }).populate('test_id').populate('user_id');
    // console.log(checkBox)
    if (checkBox == "finished") {

        let currentCompletedArr = allTestsArr.filter(item => {

            return allCompletedTestsArr.some(e => `${e.test_id._id}` == `${item._id}`);
        });

        // res.send({ allTestsArr, currentCompletedArr, allCompletedTestsArr });
        res.send({ code: 1, currentCompletedArr,allCompletedTestsArr, message: "完成的考试试卷" });
    } else if(checkBox == "unfinished"){
        // 未完成
        let currentCompletedArr = allTestsArr.filter(item => {

            return !allCompletedTestsArr.some(e => `${e.test_id._id}` == `${item._id}`);
        });

            
            // res.send({ allTestsArr, currentCompletedArr, allCompletedTestsArr });
            res.send({ code: 0, currentCompletedArr,allCompletedTestsArr, message: "未完成的考试试卷" });
            
        }else{

            let currentCompletedArr=allTestsArr;
            res.send({ code: 2, currentCompletedArr,allCompletedTestsArr, message: "全部的考试试卷" });
        }
        

}






module.exports = {
    //导出的函数名
    isOkList
}